package city.spring.modules.personnel.service.impl;

import city.spring.modules.cardinfo.entity.CardEmployeeEntity;
import city.spring.modules.cardinfo.repository.CardNumberRepository;
import city.spring.modules.cardinfo.service.CardEmployeeService;
import city.spring.modules.cardinfo.service.CardNumberService;
import city.spring.modules.personnel.entity.EmployeeEntity;
import city.spring.modules.personnel.repository.EmployeeRepository;
import city.spring.modules.personnel.service.EmployeeService;
import city.spring.modules.personnel.vo.EmployeeVO;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Service：员工信息
 *
 * @author Panxm
 * @date 2020-04-09 11:39:24
 */
@CacheConfig(cacheNames = {EmployeeServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class EmployeeServiceImpl<Private> extends ServiceImpl<EmployeeRepository, EmployeeEntity> implements EmployeeService {

    private final UserService userService;
    private final EmployeeRepository employeeRepository;
    private final CardNumberService cardNumberService;
    private final CardNumberRepository cardNumberRepository;
    private final CardEmployeeService cardEmployeeService;

    public EmployeeServiceImpl(UserService userService, CardNumberService cardNumberService, EmployeeRepository employeeRepository, CardNumberRepository cardNumberRepository, CardEmployeeService cardEmployeeService) {
        this.userService = userService;
        this.employeeRepository = employeeRepository;
        this.cardNumberService = cardNumberService;
        this.cardNumberRepository = cardNumberRepository;
        this.cardEmployeeService = cardEmployeeService;
    }

    @Override
    public void saveEmployee(EmployeeEntity entity) {
        // 设值
        entity.setState(1);
        //入职时间默认合同开始时间
        if (entity.getHiredate() == null) {
            entity.setHiredate(entity.getContractStart());
        }
        // 保存员工
        boolean save = save(entity);

        // 一卡通设置
        CardEmployeeEntity cardEmployeeEntity = new CardEmployeeEntity();
        // 会员号最大值+1
//        String maxnum = cardNumberRepository.getMaxnum().toString();
//        cardEmployeeEntity.setMemberCard(maxnum);
        // 人员类型
        cardEmployeeEntity.setPersonType(entity.getAttributes());
        // 余额初始0
        cardEmployeeEntity.setBalance(BigDecimal.valueOf(0));
        cardEmployeeEntity.setDepositBalance(BigDecimal.valueOf(0));
        // 开通时间
        cardEmployeeEntity.setOpeningTime(LocalDateTime.now());
        // 卡片的绑定人员
        cardEmployeeEntity.setPerson(entity.getId());
        cardEmployeeService.saveCardEmployee(cardEmployeeEntity);

        if (!save) {
            throw new RuntimeException("保存信息失败");
        }

        // 保存这个员工的用户帐号信息
        userService.saveUser(entity);
    }

    @Override
    public void updateEmployee(EmployeeEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
        // 修改这个员工的用户帐号信息
        userService.updateUser(entity);
    }

    @Override
    public void deleteEmployee(String primaryKey) {
        removeById(primaryKey);
        // 登陆权限 看用户表有无记录，有再删除用户表记录
        UserEntity havePerson = userService.getById(primaryKey);
        if (havePerson != null) {
            userService.deleteUser(primaryKey);
        }
    }

    @Override
    public void deleteEmployee(List<String> primaryKeys) {
        removeByIds(primaryKeys);
        // 登陆权限 删除用户表记录
        userService.deleteUser(primaryKeys);
    }

    @Override
    public List<EmployeeEntity> getScheduleEmployee(String scheduleId) {
        return baseMapper.getScheduleEmployee(scheduleId);
    }

    @Override
    public List<EmployeeEntity> getWeekEmployee(String weekId) {
        return baseMapper.getWeekEmployee(weekId);
    }

    @Override
    public Object quitJob(String id, EmployeeEntity entity) {
        entity.setState(0);
        employeeRepository.quitJob(id, entity);
        // 登陆权限 看用户表有无记录，有再限制登陆
        UserEntity userEntity = userService.getById(entity.getId());
        userEntity.setIsAccountNonLocked(false);
        return null;
    }

    @Override
    public IPage<EmployeeVO> staffInfo(Page<? extends EmployeeEntity> page, Wrapper<? extends EmployeeEntity> wrapper) {
        return employeeRepository.staffInfo(page, wrapper);
    }

    @Override
    public IPage<EmployeeVO> getPharmacistList(Page<? extends EmployeeEntity> page, Wrapper<? extends EmployeeEntity> wrapper) {
        return employeeRepository.getPharmacistList(page, wrapper);
    }

    @Override
    public IPage<EmployeeVO> getDoctorList(Page<? extends EmployeeEntity> page, Wrapper<? extends EmployeeEntity> wrapper) {
        return employeeRepository.getDoctorList(page, wrapper);
    }

}
